package luj.game.robot.internal.net.http.receive;

import luj.cluster.api.actor.Tellable;
import luj.game.robot.internal.concurrent.parent.RobotBossDependency;
import luj.game.robot.internal.net.http.collect.HandleMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class BotHttpReceiver {

  public BotHttpReceiver(String httpUri, Map<String, List<String>> httpParam, Object handleParam, HandleMap handleMap, Tellable bossRef, RobotBossDependency bossDep) {
    _httpUri = httpUri;
    _httpParam = httpParam;
    _handleParam = handleParam;
    _handleMap = handleMap;
    _bossRef = bossRef;
    _bossDep = bossDep;
  }

  public void receive() {
    HandleMap.Handler handler = _handleMap.get(_httpUri);
    if (handler == null) {
      LOG.warn("找不到URI：{}", _httpUri);
      return;
    }

    var http = new HttpImpl();
    http._param = _httpParam;

    var service = new ServiceImpl();
    service._bossRef = _bossRef;
    service._dependency = _bossDep;

    var ctx = new HandleContextImpl();
    ctx._http = http;
    ctx._handleParam = _handleParam;
    ctx._service = service;

    handler.getInstance().onHandle(ctx);
  }

  private static final Logger LOG = LoggerFactory.getLogger(BotHttpReceiver.class);

  private final String _httpUri;
  private final Map<String, List<String>> _httpParam;

  private final Object _handleParam;
  private final HandleMap _handleMap;

  private final Tellable _bossRef;
  private final RobotBossDependency _bossDep;
}
